package com.lvjunxiong.serviceinterface.click;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.map.ObjectMapper;
import utils.JsonUtils;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lvjunxiong on 2019/7/9.
 */
public class ClickAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private HttpServletResponse response;
    private ClickDao dao = new ClickDao();
    /** 三个参数：type、userid、content  **/
    private String type;
    private String userid;
    private String content;
    private String lbtid;//轮播图id

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setLbtid(String lbtid) {
        this.lbtid = lbtid;
    }

    //微信小程序点击录入日志接口
    public void clickLog() throws Exception {
        response = ServletActionContext.getResponse();
        ObjectMapper objectMapper = new ObjectMapper();
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        if(type.equals("7")){
            LbtModel mmm = dao.queryLbt(lbtid);
            if(mmm != null){
                String title = mmm.getTitle();
                int i = dao.clickLog(type, userid, "查看了"+title+"的内容");
                if (i > 0) {
                    objectMapper.writeValue(response.getWriter(), JsonUtils.getJSON("", "success", "保存日志消息内容成功！"));
                } else {
                    objectMapper.writeValue(response.getWriter(), JsonUtils.getJSON("", "fail", "保存日志消息内容失败！"));
                }
            }
        }else{
            if (type.equals("6")) {
                LbtModel mmm = dao.queryZX(lbtid);
                if (mmm != null) {
                    String title = mmm.getTitle();
                    int i = dao.clickLog(type, userid, "查看了" + title + "的内容");
                    if (i > 0) {
                        objectMapper.writeValue(response.getWriter(), JsonUtils.getJSON("", "success", "保存日志消息内容成功！"));
                    } else {
                        objectMapper.writeValue(response.getWriter(), JsonUtils.getJSON("", "fail", "保存日志消息内容失败！"));
                    }
                }
            }else {
                int i = dao.clickLog(type, userid, content);
                if (i > 0) {
                    objectMapper.writeValue(response.getWriter(), JsonUtils.getJSON("", "success", "保存日志消息内容成功！"));
                } else {
                    objectMapper.writeValue(response.getWriter(), JsonUtils.getJSON("", "fail", "保存日志消息内容失败！"));
                }
            }
        }
    }
}